package starComponent.elements;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.geom.AffineTransform;
import java.util.HashMap;
import java.util.Iterator;

import starComponent.Listeners.ListenerPopupBranch;
import starComponent.utils.StringAndPolygon;
import tuio.components.LabelTouch;

/**
 * @author Charles Perin
 * 21 avr. 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 */
public class PopupBranch extends LabelTouch{

	/**
	 * 
	 */
	private static final long serialVersionUID = -8902691848461796920L;

	private A_StarBranch branch;
	
	private HashMap<Integer, StringAndPolygon> buttonMap = new HashMap<Integer, StringAndPolygon>();

	public PopupBranch(A_StarBranch branch, int x, int y){
		this.setBranch(branch);
		branch.getStar().add(this);
		ListenerPopupBranch listener = new ListenerPopupBranch(this);
		addMouseListener(listener);
		addMouseMotionListener(listener);
		init(x,y);
		
	}
	
	protected void init(int x, int y){
		buttonMap.put(0, new StringAndPolygon("Remove", new Polygon()));
		buttonMap.put(1, new StringAndPolygon("Cancel", new Polygon()));
		
		int width = branch.getStar().getHeight()/10;

		int starWidth = (int) branch.getStar().getSize().getWidth();
		int starHeight = (int) branch.getStar().getSize().getHeight();

		FontMetrics fm = branch.getStar().getGraphics().getFontMetrics();
		int maxLength = 0;
		
		Iterator<Integer> iter = buttonMap.keySet().iterator();
		while(iter.hasNext()){
			int index = iter.next();
			int length = fm.stringWidth(buttonMap.get(index).getName());
			if(length>maxLength)maxLength=length;
		}
		
		width = Math.min(2*maxLength, width);
		
		if(x<0)x=0;
		if(y<0)y=0;
		if(x+width>starWidth) x = starWidth-width;
		if(y+buttonMap.size()*3*fm.getHeight()>starHeight) y = starHeight-buttonMap.size()*3*fm.getHeight();

		this.setBounds(x, y, width, 3*fm.getHeight()*buttonMap.size());
	}
	

	public PopupBranch(A_StarBranch a_StarBranch) {
		this.setBranch(a_StarBranch);
	}


	public void createPolygons() {
		Iterator<Integer> iter = buttonMap.keySet().iterator();
		while(iter.hasNext()){
			int index = iter.next();
			Point p1 = new Point(0, getHeight()*index/buttonMap.size());
			Point p2 = new Point(getWidth(), getHeight()*index/buttonMap.size());
			Point p3 = new Point(getWidth(), getHeight()*(index+1)/buttonMap.size());
			Point p4 = new Point(0, getHeight()*(index+1)/buttonMap.size());
						
			int[] xValues = new int[4];
			int[] yValues = new int[4];
			
			AffineTransform at = new AffineTransform();
			//si orientation du popup selon utilisateur
			if(branch.getStar().getControler().isCollaborative()){
				at.rotate(-branch.getAngle()-Math.PI/2, getWidth()/2, getHeight()/2);
				at.transform(p1, p1);
				at.transform(p2, p2);
				at.transform(p3, p3);
				at.transform(p4, p4);
			}
			
			xValues[0] = p1.x;
			xValues[1] = p2.x;
			xValues[2] = p3.x;
			xValues[3] = p4.x;
			yValues[0] = p1.y;
			yValues[1] = p2.y;
			yValues[2] = p3.y;
			yValues[3] = p4.y;
			Polygon pTransform = new Polygon(xValues, yValues, 4);
			buttonMap.get(index).setPolygon(pTransform);
			
		}
	}

	@Override
	public void paintComponent(Graphics g){
		Graphics2D g2d = (Graphics2D)g;
		
		g2d.setFont(new Font("test", Font.BOLD, g2d.getFont().getSize()+5));
		
		FontMetrics metrics = g2d.getFontMetrics();
		
		//si orientation du popup selon utilisateur
		if(branch.getStar().getControler().isCollaborative()){
			double angle = -branch.getAngle()-Math.PI/2;
			g2d.rotate(angle, this.getWidth()/2, this.getHeight()/2);
		}
		
		Iterator<Integer> iter = buttonMap.keySet().iterator();
		while(iter.hasNext()){
			int index = iter.next();
			g2d.setColor(Color.BLACK);
			g2d.drawString(buttonMap.get(index).getName(),(this.getWidth()-metrics.stringWidth(buttonMap.get(index).getName()))/2, this.getHeight()*index/buttonMap.size()+metrics.getHeight());
		}
		
		
	}
	
	public void clickOn(int x, int y) {
		Iterator<Integer> iter = buttonMap.keySet().iterator();
		while(iter.hasNext()){
			int index = iter.next();
			StringAndPolygon sas = buttonMap.get(index);
			if(sas.getPolygon().contains(x, y)){
				if(sas.getName().equals("Remove")){
					removeBranch();
				}
				else if(sas.getName().equals("Cancel")){
					closePopup();
				}
				break;
			}
		}
	}



	private void closePopup() {
		branch.getStar().remove(this);
		branch.setPopupOpen(false);
	}
	
	private void removeBranch(){
		branch.getStar().getControler().removeBranch(branch);
		branch.setPopupOpen(false);
	}


	public void setBranch(A_StarBranch branch) {
		this.branch = branch;
	}

	public A_StarBranch getBranch() {
		return branch;
	}




}
